<template>
  <div>
    <el-row>
      <el-select v-model="currentYear" @change="getCurrentChange">
        <el-option
          v-for="year in yearList"
          :key="year"
          :value="year"
          :label="year + '年'"
        />
      </el-select>
      <el-select v-model="currentMonth" @change="getCurrentChange">
        <el-option
          v-for="month in 12"
          :key="month"
          :value="month"
          :label="month + '月'"
        />
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template #dateCell="{date,data }">
        <!-- <template #dateCell="{ data }" class="content"> -->
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <!-- <span v-if="isWeek(date,data.type)" class="rest">休</span> -->
          <span v-if="isWeek(date,data)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(value) {
      //  原本的数据是2018-09-02，这里就是从 - 开始切割，拿到后面的日期数显示出来
      const day = value.split('-')[2]
      // 10号前面的数据显示有 01.。。这里就是把 0 从字符串中切割出来，通过startswitth方法,从获取的对象数据里面第一位开始切割，即把0切割，后面的正常显示即可
      // console.log(41, day)
      return day.startsWith('0') ? day.substring(1) : day
      // return day.startsWith('0') ? day.substring(1) : day
    }
  },
  data() {
    // 当前详细日期
    const currentDate = new Date()
    // 年份 计算年份
    const currentYear = currentDate
      .getFullYear()
    // 保存年份的数组
    const yearList = []
    // 计算当前月份
    const currentMonth = currentDate.getMonth() - 1
    // 节假日数组
    // const holiday = []
    // 循环当前年份计算当前年前后五年的年份
    for (let i = 0; i < 11; i++) {
      yearList.push(currentYear - 5 + i)
    }
    return {
      currentDate,
      currentYear,
      yearList,
      currentMonth,
      // 节假日数组
      holiday: ['2021-09-21', '2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04', '2021-10-05']
    }
  },
  watch: {
    // 监听点击灰色的日期进行年月的重新获取年月
    currentDate(newDate) {
      // 重新获取年月份
      // getFullYear() 方法可返回一个表示年份的 4 位数字
      this.currentYear = newDate.getFullYear()
      // 返回的月份从 0开始计算
      this.currentMonth = newDate.getMonth() + 1
    }
  },
  methods: {
    getCurrentChange() {
      // 点击切换对应的具体日期
      this.currentDate = new Date(this.currentYear + '-' + this.currentMonth)
    },
    // 判断周末添加 休 的样式
    // isWeek(date, type) {
    isWeek(date, data) {
      const weekend = (date.getDay() === 6 || date.getDay() === 0)
      // 这里的day是上面切割好的日期数据格式
      const isHoliday = this.holiday.includes(data.day)
      // console.log(87, data.day)
      // current-month 日历组件提供的一个属性
      return (weekend || isHoliday) && data.type === 'current-month'
    }
  }
}
</script>

<style  scoped>
 ::v-deep .el-calendar-day {
  height:  auto;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
</style>
